package tree;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class EvenOddTree1609 {
    //二叉树层序遍历
    public boolean isEvenOddTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> q = new LinkedList<>();
        int depth = 0;
        int levelCount = 1;
        int nextLevelCount = 0;
        int pre = Integer.MIN_VALUE;
        q.offer(root);
        while (q.size() > 0) {
            TreeNode node = q.poll();
            if (node.val % 2 == 0 && depth % 2 == 0||node.val % 2 == 1 && depth % 2 == 1) {
                return false;
            }
            if(depth%2==0&&node.val<=pre){
                    return false;
            }
            if(depth%2==1&&node.val>=pre){
                return false;
            }
            pre = node.val;
            if (node.left != null) {
                q.offer(node.left);
                nextLevelCount++;
            }
            if (node.right != null) {
                q.offer(node.right);
                nextLevelCount++;
            }
            if (--levelCount == 0) {
                depth++;
                levelCount = nextLevelCount;
                nextLevelCount = 0;
                if(depth%2==0){
                    pre = Integer.MIN_VALUE;
                }else {
                    pre = Integer.MAX_VALUE;
                }
            }
        }
        return true;

    }
}
